/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.hotelroom.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.app.hotel.service.HotelInfoService;
import me.zhengjie.modules.app.hotel.service.dto.HotelInfoDto;
import me.zhengjie.modules.app.hotelroom.domain.HoteRoomInfo;
import me.zhengjie.modules.app.hotelroom.service.HoteRoomInfoService;
import me.zhengjie.modules.app.hotelroom.service.dto.HoteRoomInfoQueryCriteria;
import me.zhengjie.modules.system.service.dto.MenuDto;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;

import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import io.swagger.annotations.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Haojie Li
* @date 2021-03-02
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "应用：酒店房间信息")
@RequestMapping("/api/hoteRoomInfo")
public class HoteRoomInfoController {

    private final HotelInfoService hotelInfoService;
    private final HoteRoomInfoService hoteRoomInfoService;

    @Log("导出酒店房间信息")
    @ApiOperation("导出酒店房间信息")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('hoteRoomInfo:list')")
    public void download(HttpServletResponse response, HoteRoomInfoQueryCriteria criteria) throws IOException {
        hoteRoomInfoService.download(hoteRoomInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询酒店房间信息")
    @ApiOperation("查询酒店房间信息")
    @PreAuthorize("@el.check('hoteRoomInfo:list')")
    public ResponseEntity<Object> query(HoteRoomInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(hoteRoomInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }
    
    

    @GetMapping("downloadExportTemplate")
    @Log("下载导入模板")
    @ApiOperation("下载导入模板")
    @PreAuthorize("@el.check('hoteRoomInfo:list')")
    public void downloadExportTemplate(HttpServletResponse response) throws IOException{
    	
    	List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		Map<String,Object> tempObj = new HashMap<String,Object>();
		tempObj.put("序号","");
		tempObj.put("酒店编号","");
		tempObj.put("酒店房间编号","");
		tempObj.put("导入状态", "");
		resultList.add(tempObj);
    	FileUtil.downloadExcel(resultList, response);
    }
    @PostMapping("export")
    @Log("导入酒店信息")
    @ApiOperation("导入酒店信息")
    @PreAuthorize("@el.check('hoteRoomInfo:list')")
    public void export(@RequestBody Map<String,Object> param, HttpServletResponse response) throws IOException{
    	String exportFilePath = (String)param.get("exportFilePath");
    	System.out.println(exportFilePath);
    	
    	//读取文件内容
    	ExcelReader excelReader = ExcelUtil.getReader(new File(exportFilePath));
    	
    	List<Map<String, Object>> listAll = excelReader.readAll();
    	List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
    	
    	for(int index = 0; index < listAll.size(); index ++) {
    		Map<String,Object> tempObj = listAll.get(index);
    		String hotelNo = tempObj.get("酒店编号").toString();
    		String hotelRoomNo = tempObj.get("酒店房间编号").toString();
    		
    		//添加酒店房间信息
    		tempObj.put("导入状态", "导入成功");
    		HotelInfoDto hotelInfoDto = hotelInfoService.findByHotelNo(hotelNo);
    		if(hotelInfoDto == null) {
    			tempObj.put("导入状态", "导入失败，酒店编号不存在");
    			resultList.add(tempObj);
    			continue;
    		}else {
    			
        		if(hoteRoomInfoService.isExistByHotelNo(hotelInfoDto.getId(), hotelRoomNo)) {
        			tempObj.put("导入状态", "导入失败，酒店房间编号已存在");
        			resultList.add(tempObj);
        			continue;
            	}
        		
        		HoteRoomInfo resources = new HoteRoomInfo();
        		resources.setHoteId(hotelInfoDto.getId());
        		resources.setHoteName(hotelInfoDto.getHotelName());
        		resources.setRoomNo(hotelRoomNo);
        		
        		hoteRoomInfoService.create(resources);
        		resultList.add(tempObj);
    		}
    	}
    	FileUtil.downloadExcel(resultList, response);
    }

    @PostMapping
    @Log("新增酒店房间信息")
    @ApiOperation("新增酒店房间信息")
    @PreAuthorize("@el.check('hoteRoomInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody HoteRoomInfo resources){
    	if(hoteRoomInfoService.isExistByHotelNo(resources.getHoteId(), resources.getRoomNo())) {
    		throw new BadRequestException("酒店房间编号已存在");
    	}
        return new ResponseEntity<>(hoteRoomInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改酒店房间信息")
    @ApiOperation("修改酒店房间信息")
    @PreAuthorize("@el.check('hoteRoomInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody HoteRoomInfo resources){
        hoteRoomInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除酒店房间信息")
    @ApiOperation("删除酒店房间信息")
    @PreAuthorize("@el.check('hoteRoomInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        hoteRoomInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}